Frequently, when an experiment is performed, we are interested mainly in some function of the outcome as opposed to the actual outcome itself.
For instance,
1) In recent flipping a coin experiment, we may be interested in the total number of heads that occur and not care at all about the actual Head(H)–Tail(T) sequence that results.
2) In throwing dice, we are often interested in the sum of the two dice and are not really concerned about the separate values of each die. That is, we may be interested in knowingthat the sum is 7 and may not be concerned over whether the actual outcome was: (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), or (6, 1).
Also, These quantities of interest, or, more formally, these real-valued functions defined on the sample space, are known as 'Random Variables'.
In [64]:
%matplotlib inline
import numpy as np
import pandas as pd
from itertools import product
# from IPython.core.display import HTML
# css = open('media/style-table.css').read() + open('media/style-notebook.css').read()
# HTML('<style>{}</style>'.format(css))
In [2]:
one_toss = np.array(['H', 'T'])
In [3]:
two_tosses = list(product(one_toss, repeat=2))
two_tosses
Out[3]:
In [4]:
# For three tosses, just change the number of repetitions:
three_tosses = list(product(one_toss, repeat=3))
three_tosses
Out[4]:
As shown earlier in slide,
A probability space $(\Omega, P)$ is an outcome space accompanied by the probabilities of all the outcomes.
If you assume all eight outcomes of three tosses are equally likely, the probabilities are all 1/8:
In [5]:
three_toss_probs = (1/8)*np.ones(8)
In [6]:
three_toss_space = pd.DataFrame({
'Omega':three_tosses,
'P(omega)':three_toss_probs
})
three_toss_space
Out[6]:
As you can see above, Product spaces(Probability spaces) get large very quickly.
If we are tossing 10 times, the outcome space would consist of the $2^{10}$ sequences of 10 elements where each element is H or T.
The outcomes are a pain to list by hand, but computers are good at saving us that kind of pain.
Lets take example of rolling die,
If we roll a die 5 times, there are almost 8,000 possible outcomes:
In [7]:
die = np.arange(1, 7, 1)
five_rolls = list(product(die, repeat=5))
# five_rolls = [list(i) for i in product(die, repeat=5)]
five_roll_probs = (1/6**5)**np.ones(6**5)
five_roll_space = pd.DataFrame({
'Omega':five_rolls,
'P(omega)':five_roll_probs
})
five_roll_space
Out[7]:
Suppose you roll a die five times and add up the number of spots you see. If that seems artificial, be patient for a moment and you'll soon see why it's interesting.
The sum of the rolls is a numerical function on the outcome space $\Omega$ of five rolls. The sum is thus a random variable. Let's call it $S$ . Then, formally, $S: \Omega \rightarrow \{ 5, 6, \ldots, 30 \}$
The range of $S$ is the integers 5 through 30, because each die shows at least one and at most six spots. We can also use the equivalent notation
$\Omega \stackrel{S}{\rightarrow} \{ 5, 6, \ldots, 30 \}$
From a computational perspective, the elements of $\Omega$ are in the column omega of five_roll_space. Let's apply this function and create a larger table.
In [8]:
five_rolls_sum = pd.DataFrame({
'Omega':five_rolls,
'S(omega)':five_roll_space['Omega'].map(lambda val: sum(val)),
'P(omega)':five_roll_probs
})
five_rolls_sum
Out[8]:
A random variable is a numerical function on $\Omega$ . Therefore by composition, a numerical function of a random variable is also a random variable.
For example, $S^2$ is a random variable, calculated as follows:
$S^2(\omega) = \big{(} S(\omega)\big{)}^2$
Thus for example $S^2(\text{[6 6 6 6 6]}) = 30^2 = 900$.
From the table five_rolls_sum it is hard to tell how many rows show a sum of 6, or 10, or any other value. To better understand the properties of $S$, we have to organize the information in five_rolls_sum.
For any subset $A$ of the range of $S$, define the event $\{S \in A\}$ as
$$ \{S \in A \} = \{\omega: S(\omega) \in A \} $$That is, $\{ S \in A\}$ is the collection of all $\omega$ for which $S(\omega)$ is in $A$.
If that definition looks unfriendly, try it out in a special case. Take $A = \{5, 30\}$. Then $\{S \in A\}$ if and only if either all the rolls show 1 spot or all the rolls show 6 spots. So $$ \{S \in A\} = \{\text{[1 1 1 1 1], [6 6 6 6 6]}\} $$
It is natural to ask about the chance the sum is a particular value, say 10. That's not easy to read off the table, but we can access the corresponding rows:
In [9]:
five_rolls_sum[five_rolls_sum['S(omega)']==10]
Out[9]:
There are 126 values of $\omega$ for which $S(\omega) = 10$. Since all the $\omega$ are equally likely, the chance that $S$ has the value 10 is 126/7776.
We are informal with notation and write $\{ S = 10 \}$ instead of $\{ S \in \{10\} \}$: $$ P(S = 10) = \frac{126}{7776} = 1.62\% $$
i.e., Random variables provide numerical summaries of the experiment in question. - Stats110 harvard (also below paragraph)
This definition is abstract but fundamental; one of the most important skills to develop when studying probability and statistics is the ability to go back and forth between abstract ideas and concrete examples. Relatedly, it is important to work on recognizing the essential pattern or structure of a problem and how it connectsto problems you have studied previously. We will often discuss stories that involve tossing coins or drawing balls from urns because they are simple, convenient sce- narios to work with, but many other problems are isomorphic: they have the same essential structure, but in a different guise.
we can use mathematical opeartion on these variables since they are real valued function nowto problems you have studied previously. We will often discuss stories that involve tossing coins or drawing balls from urns because they are simple, convenient sce- narios to work with, but many other problems are isomorphic: they have the same essential structure, but in a di↵erent guise.
The table below shows all the possible values of $S$ along with all their probabilities. It is called a "Probability Distribution Table" for $S$ .
In [119]:
dist_S = five_rolls_sum.drop('Omega', axis=1).groupby('S(omega)', as_index=False).sum()
dist_S
Out[119]:
The contents of the table – all the possible values of the random variable, along with all their probabilities – are called the probability distribution of $S$ , or just distribution of $S$ for short. The distribution shows how the total probability of 100% is distributed over all the possible values of $S$ .
Let's check this, to make sure that all the $\omega$ 's in the outcome space have been accounted for in the column of probabilities.
In [123]:
dist_S.ix[:,1].sum()
Out[123]:
That's 1 in a computing environment, and it is true in general for the distribution of any random variable.
Probabilities in a distribution are non-negative and sum to 1.
In [126]:
dist_S.ix[:,0], dist_S.ix[:,1]
Out[126]:
In [127]:
s = dist_S.ix[:,0]
p_s = dist_S.ix[:,1]
In [136]:
dist_S = pd.concat([s,p_s],axis=1)
dist_S
Out[136]:
In [149]:
dist_S.plot(x="S(omega)",y="P(omega)", kind="bar")
Out[149]:
In [166]:
from prob140 import Plot
In [165]:
!pip install sympy
In [ ]: